/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modele;


import exception.AuthenticationLoginFailure;
import exception.AuthenticationPasswordFailure;
import exception.CustomerNotRegistered;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

/**
 *
 * @author Alex
 */
public class Company {
    protected Integer idCompany;
    protected String name;
    protected Collection<Customer> customers;
    protected Collection<Department> departments;
    protected Collection<Garage> affiliatedGarages;
    protected Collection<Claim> registeredClaims;
    protected Collection<Claim> classifiedClaims;
    protected Collection<Claim> formFilledClaims;
    protected Collection<Claim> validatedClaims;
    protected Collection<Claim> validatedForPaymentClaims;
    protected Collection<Paiement> paiementsRequested;
    protected Collection<Paiement> paiementsDone;

    public Company(String name) {
        this.name = name;
        this.customers = new HashSet<>();
        this.departments = new HashSet<>();
        this.affiliatedGarages = new HashSet<>();
        this.registeredClaims = new ArrayList<>();
        this.classifiedClaims = new ArrayList<>();
        this.formFilledClaims = new ArrayList<>();
        this.validatedClaims = new ArrayList<>();
        this.validatedForPaymentClaims = new ArrayList<>();
        this.paiementsRequested = new ArrayList<>();
        this.paiementsDone = new ArrayList<>();
    }

    public Integer getIdCompany() {
        return idCompany;
    }

    public void setIdCompany(Integer idCompany) {
        this.idCompany = idCompany;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addDepartment(Department dpt) {
        /* Deal with exceptions (already existing, etc.) */
        departments.add(dpt);
    }
    
    public void delDepartment(Department dpt) {
        /* Deal with exceptions (not found, etc.) */
        departments.remove(dpt);
    }

    public Collection<Department> getDepartments() {
        return departments;
    }

    public Collection<Garage> getAffiliatedGarages() {
        return affiliatedGarages;
    }
    
    public void addGarage( Garage garage ) {
        /* Deal with exceptions (already existing, etc.) */
        affiliatedGarages.add(garage);
    }
    
    public void delGarage( Garage garage ) {
        /* Deal with exceptions (not found, etc.) */
        affiliatedGarages.remove(garage);
    }

    public Employee authenticateUser(String login, String password)
            throws AuthenticationPasswordFailure, AuthenticationLoginFailure {
        for (Department d : this.departments) {
            for (Employee e : d.getEmployees()) {
                if (e.getLogin().equals(login)) {
                    if (e.getPassword().equals(password)) {
                        /* Authentication granted */
                        return e;
                    }
                    else {
                        /* Authentication refused */
                        throw new AuthenticationPasswordFailure();
                    }
                }
            }
        }
        throw new AuthenticationLoginFailure(login);
    }
    
    public Collection<Customer> getCustomers() {
        return customers;        
    }
    
    public Customer getCustomer(Integer id) throws CustomerNotRegistered {
        for (Customer customer : this.customers) {
            if (customer.getIdCustomer().equals(id)) {
                return customer;
            }
        }
        throw new CustomerNotRegistered();
    }

    public void addCustomer(Customer customer) {
        customers.add(customer);
    }
    
    public void delCustomer(Customer customer) {
        customers.remove(customer);
    }

    public Collection<Claim> getRegisteredClaims() {
        return registeredClaims;
    }
    
    public void registerClaim(Claim claim) {
        registeredClaims.add(claim);
    }

    public Object getNumberRegisteredClaims() {
        return registeredClaims.size();
    }

    public Collection<Claim> getValidatedClaims() {
        return validatedClaims;
    }
    
    public void formFillAndUnClassifyClaim(Claim claim) {
        this.classifiedClaims.remove(claim);
        this.formFilledClaims.add(claim);
    }
    
    public void validateAndUnFormFilledClaim(Claim claim) {
        this.formFilledClaims.remove(claim);
        this.validatedClaims.add(claim);
    }

    public void validateForPaymentAndUnValidateClaim(Claim claim) {
        this.validatedClaims.remove(claim);
        this.validatedForPaymentClaims.add(claim);
    }
    
    public Collection<Claim> getClassifiedClaims() {
        return classifiedClaims;
    }

    public Collection<Claim> getFormFilledClaims() {
        return formFilledClaims;
    }
    
    public void addFormFilledClaim(Claim claim) {
        this.formFilledClaims.add(claim);
    }
    
    public Collection<Claim> getValidatedForPaymentClaims() {
        return validatedForPaymentClaims;
    }


    public void replaceClaimForClassification(Claim oldClaim, Claim newClaim) {
        registeredClaims.remove(oldClaim);
        classifiedClaims.add(newClaim);
    }

    public Collection<Paiement> getPaiementsRequested() {
        return paiementsRequested;
    }
    
    public void requestPaiement( Paiement paiement ) {
        paiementsRequested.add(paiement);
    }
    
    public void processPaiement( Paiement paiement ) {
        paiementsDone.add(paiement);
        paiementsRequested.remove(paiement);
    }

    public Collection<Paiement> getPaiementsDone() {
        return paiementsDone;
    }

    public void setPaiementsDone(Collection<Paiement> paiementsDone) {
        this.paiementsDone = paiementsDone;
    }
}
